package com.czn.shopdemo.controller;

import com.czn.shopdemo.model.Order;
import com.czn.shopdemo.model.Result;
import com.czn.shopdemo.service.OrderService;
import com.czn.shopdemo.utils.TokenInfoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Zenan Chen
 * @date 2023/4/6 9:02
 */
@RestController
@RequestMapping("/order")
@Api(tags = "订单管理")
public class OrderController {
    private static final Logger log = LoggerFactory.getLogger(OrderController.class);
    @Autowired
    private OrderService orderService;

    @ApiOperation(value = "获取订单详情", notes = "获取订单详情")
    @PostMapping(value = "getOrderDetail")
    @ResponseBody
    public Object getOrderDetail(@RequestParam String orderId) {
        try {
            log.info("------------------------------------->/getOrderDetail 获取订单详情开始");
            Order order = orderService.getOrderDetail(orderId);
            log.info("------------------------------------->/getOrderDetail 获取订单详情结束");
            return Result.ok(order);
        } catch (Exception e) {
            log.error("------------------------->/createOrder 获取订单详情异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }


    @ApiOperation(value = "获取订单列表", notes = "获取订单列表")
    @PostMapping(value = "getOrderList")
    @ResponseBody
    public Object getOrderList(HttpServletRequest request) {
        try {
            log.info("------------------------------------->/getOrderList 获取订单列表开始");
            Long userId = TokenInfoUtil.getUserId(request);
            List<Order> result = orderService.getOrderList(String.valueOf(userId));
            log.info("------------------------------------->/getOrderList 获取订单列表结束");
            return Result.ok(result);
        } catch (Exception e) {
            log.error("------------------------->/createOrder 获取订单列表异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }

    @ApiOperation(value = "取消下单", notes = "取消下单")
    @PostMapping(value = "cancelOrder")
    @ResponseBody
    public Object cancelOrder(@RequestParam String orderId) {
        try {
            log.info("------------------------------------->/cancelOrder 取消下单开始");
            orderService.cancelOrder(orderId);
            log.info("------------------------------------->/cancelOrder 取消下单结束");
            return Result.ok("取消下单成功！");
        } catch (Exception e) {
            log.error("------------------------->/createOrder 取消下单异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }

    @ApiOperation(value = "下单", notes = "下单")
    @PostMapping(value = "createOrder")
    @ResponseBody
    public Object createOrder(@RequestBody Order order, HttpServletRequest request) {
        try {
            log.info("------------------------------------->/createOrder 下单开始");
            orderService.createOrder(order, request);
            log.info("------------------------------------->/createOrder 下单结束");
            return Result.ok("下单成功！");
        } catch (Exception e) {
            log.error("------------------------->/createOrder 下单异常【{}】", e.getStackTrace());
            return Result.build(201, e.getMessage());
        }
    }


}
